;(function(){
  'use strict';
  angular.module('services.omdisplay.upayQM')
    .factory('FullAbnormalServer',fullAbnormalServer)
    .factory('UpayFullAbnorSelecotor',upayFullAbnorSelecotor)
    .factory('UpayFullAbnorQueryParam',upayFullAbnorQueryParam)
    .factory('UpayFullAbnorViewObj',upayFullAbnorViewObj);
  function fullAbnormalServer(Restangular){
    return{
      fullAbnormalQuery : fullAbnormalQuery
    };
    
    function fullAbnormalQuery(param,callback){
      return Restangular.all('sas/OMdisplay/upayQM/businessQM/allAbnormal').post(param).then(callback);
    }
  }

  fullAbnormalServer.$inject = ['Restangular'];

  function upayFullAbnorSelecotor(COREDATA,UpayQMServer,UpayFullAbnorQueryParam){
    var selector = {
      businessLine : COREDATA.BUSINESSLINE,
      timeType : COREDATA.TIMETYPES,
      agencyType : [{name:'省公司',code:'0'},{name:'银行',code:'1'}]
    };
    return {
      selector : selector,
      setSelector : setSelector
    };

    function setSelector(agencyType){
      var banks = UpayQMServer.getBank();
      var province = COREDATA.PROVINCES;
      if(agencyType === '1'){
        selector.agencys = banks;
      }else{
        selector.agencys = province.map(function(item){ return {agencyName:item.provinceName,agencyCode:item.provinceCode};});
      }
      UpayFullAbnorQueryParam.resetAgency();
      return selector;
    }
  }

  upayFullAbnorSelecotor.$inject = ['COREDATA','UpayQMServer','UpayFullAbnorQueryParam'];

  function upayFullAbnorQueryParam(UpayQMServer,CoreService,UpayFullAbnorViewObj,FullAbnormalServer){
    var param = {};
    var date = {};
    var flag = {};

    return{
      initFlag : initFlag,
      initDate : initDate,
      initParam : initParam,
      resetAgency : resetAgency,
      fullAbnorQueryFn : fullAbnorQueryFn
    };

    function initFlag(){
      flag = {
        formIsFold : false,
        isQuery : false
      };
      return flag;
    }

    function initParam(){
      param = {
        quarter:{}
      };
      return param;
    }

    function initDate(){
      if(is.not.undefined(param.timeType)){
        date = UpayQMServer.initDate(param.timeType.timeTypeCode);
      }else{
        date = UpayQMServer.initDate('1');
      }
      return date;
    }

    function resetAgency(){
      param.agencyCode = [];
      return param.agencyCode;
    }
    
    function fullAbnorQueryFn(){
      var tempParam = {};
      if(!flag.formIsFold){
        tempParam = setParam();
        FullAbnormalServer.fullAbnormalQuery(tempParam,function(res){
          if(res.state !== '10000' || checkData(res.data)){
            CoreService.dialog(res.state);
            return;
          }
          UpayFullAbnorViewObj.setSeries(res.data);
          UpayFullAbnorViewObj.setExportParam(tempParam);
          flag.formIsFold = !flag.formIsFold;
          flag.isQuery = true;
        });
      }else{
        flag.formIsFold =! flag.formIsFold;
      }
    }

    function setParam(){
      var tempParam = {};
      var formatArr = [
        undefined,'yyyy-MM-dd',undefined,'yyyy-MM-01','yyyy-01-01','yyyy-01-01'
      ];
      var format;
      if(param.timeType){
        tempParam.timeType = param.timeType.timeTypeCode;
      }else{
        tempParam.timeType = '1';
      }
      format = formatArr[tempParam.timeType];
      if(param.timeType){
        switch(param.timeType.timeTypeCode){
          case '4':
            tempParam.startTime = param.quarter.startTime.substr(0,4)+'-01-01';
            tempParam.endTime = param.quarter.endTime.substr(0,4)+'-01-01';
            tempParam.startQuarter = param.quarter.startTime.substr(4,1);
            tempParam.endQuarter = param.quarter.endTime.substr(4,1);
            break;
          case '5':
            tempParam.startTime = date.startTime+'-01-01';
            tempParam.endTime = date.endTime+'-01-01';
            break;
          default :
            tempParam.startTime = CoreService.dateFormat(date.startTime,format);
            tempParam.endTime = CoreService.dateFormat(date.endTime,format);
        }
      }else{
        tempParam.startTime = CoreService.dateFormat(date.startTime,format);
        tempParam.endTime = CoreService.dateFormat(date.endTime,format);
      }
      return tempParam;
    }

    function checkData(data){
      var isNull = true;
      if(data && is.array(data) && data.length !== 0){
        isNull = false;
      }
      return isNull;
    }
  }

  function upayFullAbnorViewObj(){
    var viewObj = {};
    return {
      initViewObj : initViewObj,
      setSeries : setSeries,
      setExportParam : setExportParam
    };

    function initViewObj(){
      viewObj = {
        series : [],
        exportParam :{}
      };
      return viewObj;
    }

    function setSeries(series){
      viewObj.series = series;
      return viewObj.series;
    }

    function setExportParam(param){
      viewObj.exportParam = param;
      return viewObj.exportParam;
    }
  }

  upayFullAbnorQueryParam.$inject = ['UpayQMServer','CoreService','UpayFullAbnorViewObj','FullAbnormalServer'];
})();